//
//  MYGMedicalToolsCalorieManager.swift
//  MYGrow
//
//  Created by 林矗 on 2024/4/11.
//

import UIKit

class MYGMedicalToolsCalorieManager: NSObject {
    static let `shareManager` = MYGMedicalToolsCalorieManager()
    func loadData(with date: Date, completion: @escaping ((MYGMedicalToolsCalorieFoodRecordModel?) -> ())) {
        MYGNetwork<MYGMedicalToolsCalorieApi, MYGMedicalToolsCalorieFoodRecordModel>.request(target: .records(date: date)) { result in
            completion(result)
        } failed: { _, _ in
            completion(nil)
        }
    }
    
    func loadSportData(with date: Date, completion: @escaping ((MYGMedicalToolsCalorieSportRecordModel?) -> ())) {
        MYGNetwork<MYGMedicalToolsCalorieApi, MYGMedicalToolsCalorieSportRecordModel>.request(target: .sportRecords(date: date)) { result in
            completion(result)
        } failed: { _, _ in
            completion(nil)
        }
    }
    
    func loadTodayFoodAndSportData(completion: @escaping ((MYGMedicalToolsCalorieFoodRecordModel?, MYGMedicalToolsCalorieSportRecordModel?) -> ())) {
        let group = DispatchGroup()
        group.enter()
        var tuple: (MYGMedicalToolsCalorieFoodRecordModel?, MYGMedicalToolsCalorieSportRecordModel?) = (nil, nil)
        loadData(with: Date()) { food in
            tuple.0 = food
            group.leave()
        }
        group.enter()
        loadSportData(with: Date()) { sport in
            tuple.1 = sport
            group.leave()
        }
        
        group.notify(queue: DispatchQueue.main) {
            completion(tuple.0, tuple.1)
        }
    }
}
